﻿using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.XtraEditors;
using System.Windows.Forms;
using System.Data;
using VietKinsNutrition.Data;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;

namespace VietKinsNutrition.Bussiness
{
    public class ClsTblDm_NhucaudinhduongotruongBLL
    {

        ClsTblDm_NhucaudinhduongotruongDAL ClsTblDm_Nhucaudinhduongotruongdal = new ClsTblDm_NhucaudinhduongotruongDAL();

        private int[] _Rows;
        private int _j;
        private int _idDinhduong;
        private int[] _Id;
        private DataTable _Table;
        private DataRow _Row;
        private int n;

        public int idDinhDuong
        {
            set { _idDinhduong = value; }
        }
        public int[] Id
        {
            set { _Id = value; }
        }
        public int[] Rows
        {
            set { _Rows = value; }
        }

        public int j
        {
            set { _j = value; }
        }

        public ClsTblDm_NhucaudinhduongotruongBLL()
        {
            int k;
            k = ClsTblDm_Nhucaudinhduongotruongdal.GetTable(QueryString.TBLDM_KHOILOP_SELECTALL, false).Rows.Count;
            _Rows = new int[k];
            _Id = new int[k];
            _j = 0;
            n = 0;
        }
        private bool CheckTable(DataTable Table, string Chuoi)
        {
            foreach (DataRow dr in Table.Rows)
            {
                if (dr["Tenkhoilop"].ToString().Trim() == Chuoi.Trim())
                {
                    return false;
                }
            }
            return true;
        }
        public void LoadData(GridView gridView, bool Vungmien)
        {
            int j = 1;
            _Table = new DataTable();
            if (_Table.Columns.Count == 0)
            {
                for (int i = 0; i < gridView.Columns.Count; i++)
                {
                    _Table.Columns.Add(gridView.Columns[i].Name, typeof(System.String));
                }
            }
            DataTable table;
            if (Vungmien.ToString().ToUpper() == "TRUE")
            {
                table = ClsTblDm_Nhucaudinhduongotruongdal.GetTable(QueryString.PRCTBLDM_NHUCAUDINHDUONGOTRUONG_SELECALL.Replace("@Vungmien", "-1"), Vungmien);
            }
            else
            { table = ClsTblDm_Nhucaudinhduongotruongdal.GetTable(QueryString.PRCTBLDM_NHUCAUDINHDUONGOTRUONG_SELECALL.Replace("@Vungmien", "0"), Vungmien); }
            foreach (DataRow dr in table.Rows)
            {
                if (CheckTable(_Table, dr["Tenkhoilop"].ToString().Trim()))
                {
                    _Row = _Table.NewRow();
                    _Row["STT"] = j;
                    _Row["Tenkhoilop"] = dr["Tenkhoilop"].ToString();
                    _Row["KhoilopId"] = dr["KhoilopId"].ToString();
                    _Row["KCaloCd"] = dr["KCaloCd"].ToString();
                    _Row["KCaloCtr"] = dr["KCaloCtr"].ToString();
                    _Row["ProtidCd"] = dr["ProtidCd"].ToString();
                    _Row["ProtidCtr"] = dr["ProtidCtr"].ToString();
                    _Row["LipidCd"] = dr["LipidCd"].ToString();
                    _Row["LipidCtr"] = dr["LipidCtr"].ToString();
                    _Row["GlucidCd"] = dr["GlucidCd"].ToString();
                    _Row["GlucidCtr"] = dr["GlucidCtr"].ToString();
                    _Row["Id"] = dr["Id"].ToString();
                    _Row["ColumnVungmien"] = dr["vungmien"].ToString();
                    _Table.Rows.Add(_Row);
                    j = j + 1;
                }
            }
            gridView.GridControl.DataSource = _Table;
        }

        public void DataGridView_Click(DataGridView gridView)
        {
            if (gridView.CurrentRow.Cells[0].Value != null)
            {
                _idDinhduong = int.Parse(gridView.CurrentRow.Cells["KhoilopId"].Value.ToString());
                if (CheckInput(_Id, _idDinhduong))
                {
                    _Id[_j] = _idDinhduong;
                    _Rows[_j] = gridView.CurrentRow.Index;
                    _j = _j + 1;
                }
            }
        }

        public void DataBase(int Option, bool Vungmien, GridView gridView)
        {
            bool Ok = false;
            switch (Option)
            {
                case 1:
                    {
                        string[] Value = new string[] { "@KhoilopId", "@KCaloCd", "@KCaloCtr", "@ProtidCd", "@ProtidCtr", "@LipidCd", "@LipidCtr", "@GlucidCd", "@GlucidCtr" };
                        DataTable dtKL;
                        DataTable dtNC;
                        dtKL = ClsTblDm_Nhucaudinhduongotruongdal.GetTable(QueryString.TBLDM_KHOILOP_SELECTALL, Vungmien);
                        if (Vungmien.ToString().ToUpper() == "TRUE")
                        {
                            dtNC = ClsTblDm_Nhucaudinhduongotruongdal.GetTable(QueryString.PRCTBLDM_NHUCAUDINHDUONGOTRUONG_GET.Replace("@Vungmien", "-1"), Vungmien);
                        }
                        else
                        { dtNC = ClsTblDm_Nhucaudinhduongotruongdal.GetTable(QueryString.PRCTBLDM_NHUCAUDINHDUONGOTRUONG_GET.Replace("@Vungmien", "0"), Vungmien); }
                        foreach (DataRow dr1 in dtKL.Rows)
                        {
                            foreach (DataRow dr2 in dtNC.Rows)
                            {
                                if (int.Parse(dr1["Id"].ToString().Trim()) == int.Parse(dr2["KhoilopId"].ToString()))
                                { Ok = true; }
                            }
                            if (Ok == false)
                            {
                                float[] Values = new float[] { float.Parse(dr1["Id"].ToString()), 0, 0, 0, 0, 0, 0, 0, 0 };
                                ClsTblDm_Nhucaudinhduongotruongdal.InsertData("PrcTblDm_Nhucaudinhduongotruong_Insert", Value, Values, Vungmien, 0);
                            }
                            Ok = false;
                        }
                        break;
                    }
                case 2:
                    {
                        if (XtraMessageBox.Show("Bạn có chắc chắn muốn cập nhật không ? ", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                        {

                            string[] Value = new string[] { "@KhoilopId", "@KCaloCd", "@KCaloCtr", "@ProtidCd", "@ProtidCtr", "@LipidCd", "@LipidCtr", "@GlucidCd", "@GlucidCtr" };
                            for (_j = 0; _j < gridView.RowCount; _j++)
                            {
                                ClsTblDm_Nhucaudinhduongotruongdal.InsertData(QueryString.PRCTBLDM_NHUCAUDINHDUONGOTRUONG_UPDATE, Value, RetturnValues(_j, gridView), Vungmien, int.Parse(gridView.GetRowCellValue(_j, "Id").ToString()));
                            }
                        }
                        break;
                    }
            }
        }
        public bool CheckInput(int[] IdLuu, int Id)
        {
            for (int i = 0; i < IdLuu.Length; i++)
            {
                if (IdLuu[i] == Id)
                {
                    return false;
                }
            }
            return true;
        }

        public float[] RetturnValues(int CurrentRow, GridView gridView)
        {

            float[] Temp = new float[9];
            Temp[0] = float.Parse(gridView.GetRowCellValue(CurrentRow, "KhoilopId").ToString());

            if (gridView.GetRowCellValue(CurrentRow, "KCaloCd").ToString() == "")
            {
                Temp[1] = 0;
            }
            else
            {
                Temp[1] = float.Parse(gridView.GetRowCellValue(CurrentRow, "KCaloCd").ToString());
            }

            if (gridView.GetRowCellValue(CurrentRow, "KCaloCtr").ToString() == "")
            { Temp[2] = 0; }
            else { Temp[2] = float.Parse(gridView.GetRowCellValue(CurrentRow, "KCaloCtr").ToString()); }

            if (gridView.GetRowCellValue(CurrentRow, "ProtidCd").ToString() == "")
            { Temp[3] = 0; }
            else { Temp[3] = float.Parse(gridView.GetRowCellValue(CurrentRow, "ProtidCd").ToString()); }

            if (gridView.GetRowCellValue(CurrentRow, "ProtidCtr").ToString() == "")
            { Temp[4] = 0; }
            else { Temp[4] = float.Parse(gridView.GetRowCellValue(CurrentRow, "ProtidCtr").ToString()); }

            if (gridView.GetRowCellValue(CurrentRow, "LipidCd").ToString() == "")
            { Temp[5] = 0; }
            else { Temp[5] = float.Parse(gridView.GetRowCellValue(CurrentRow, "LipidCd").ToString()); }

            if (gridView.GetRowCellValue(CurrentRow, "LipidCtr").ToString() == "")
            { Temp[6] = 0; }
            else { Temp[6] = float.Parse(gridView.GetRowCellValue(CurrentRow, "LipidCtr").ToString()); }

            if (gridView.GetRowCellValue(CurrentRow, "GlucidCd").ToString() == "")
            { Temp[7] = 0; }
            else { Temp[7] = float.Parse(gridView.GetRowCellValue(CurrentRow, "GlucidCd").ToString()); }

            if (gridView.GetRowCellValue(CurrentRow, "GlucidCtr").ToString() == "")
            { Temp[8] = 0; }
            else { Temp[8] = float.Parse(gridView.GetRowCellValue(CurrentRow, "GlucidCtr").ToString()); }

            return Temp;
        }
        public void Cell_EndEdit(GridView girdView)
        {

            if (girdView.GetFocusedValue() != null)
            {
                DataGridViewCell cuCell = (DataGridViewCell)girdView.GetFocusedValue();
                string mainStr = girdView.GetFocusedValue().ToString();
                float k;
                if (float.TryParse(mainStr, out k) == false)
                {
                    XtraMessageBox.Show("Không đúng định dạng dữ liệu", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    girdView.SetFocusedValue(0);
                    girdView.ClearSelection();
                    girdView.SetFocusedValue(cuCell);
                    girdView.SelectCell((GridCell)girdView.GetFocusedValue());
                }
                if (mainStr.Length == 0)
                {
                    girdView.SetFocusedValue(0);
                }
            }
        }
    }
}
